#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QTime>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlRecord>




Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    tcpServer = NULL;
    tcpSocket = NULL;

    //监听套接字，指定父对象，让其自动回收空间
    tcpServer = new QTcpServer(this);

    tcpServer->listen(QHostAddress::Any, 5556);

    setWindowTitle("服务器");





    connect(tcpServer, &QTcpServer::newConnection,
            [=]()
            {
                //取出建立好连接的套接字
                tcpSocket = tcpServer->nextPendingConnection();

                connect(tcpSocket, &QTcpSocket::readyRead,
                        [=]()
                        {
                            //从通信套接字中取出内容
                            QByteArray array = tcpSocket->readAll();
                            QString str = QString(array);
                            QString namemd5 = str.mid(0,16);
                            QString pwdmd5 = str.mid(16);
                            if(namemd5 =="63a9f0ea7bb98050"&&pwdmd5== "ae60c60815927ed9"){
                                //生成秘钥
                                int max = 8;
                                QString tmp = QString("0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ");
                                QString str = QString();
                                QTime t;
                                t= QTime::currentTime();
                                qsrand(t.msec()+t.second()*1000);
                                for(int i=0;i<max;i++) {
                                    int ir = qrand()%tmp.length();
                                    str[i] = tmp.at(ir);
                                }
                                //打开数据库存储秘钥
                                QSqlDatabase db1 = QSqlDatabase::addDatabase("QMYSQL","a");

                                db1.setHostName("127.0.0.1");

                                db1.setPort(3306);

                                db1.setUserName("root");

                                db1.setPassword("19751220");

                                db1.setDatabaseName("qt");

                                if(!db1.open()){
                                    QString re = "打开数据库失败";
                                    tcpSocket->write( re.toUtf8().data() );
                                    return ;
                                }
                                QSqlQuery query1(db1);
                                query1.exec("UPDATE md5 SET md5='"+str+"'");
                                db1.close();
                                //返回秘钥
                                tcpSocket->write( str.toUtf8().data() );
                            }else{
                                int max = 20;
                                QString tmp = QString("0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ");
                                QString strmax = QString();
                                QTime t;
                                t= QTime::currentTime();
                                qsrand(t.msec()+t.second()*1000);
                                for(int i=0;i<max;i++) {
                                    int ir = qrand()%tmp.length();
                                    strmax[i] = tmp.at(ir);
                                }
                                tcpSocket->write( strmax.toUtf8().data() );
                            }



                            //tcpSocket->write( str3.toUtf8().dataA() );
                        }

                        );
            }
            );





}

Widget::~Widget()
{


    delete ui;


}



